﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WellBuilt.Model;

namespace WellBuilt.Service
{
    public class CraneBeamService
    {
        internal static CraneBeamOut GirderResult(CraneBeamIn craneBeamIn)
        {
            //上翼缘正应力=IF(D21<G11,"OK","NO")
            double G4 = craneBeamIn.DC_Tread;
            double A4 = craneBeamIn.DC_Span;
            double G5 = G4 < A4 / 2 ? G4 / 4 : 0;
            double G6 = A4 / 2 - G5;
            double G7 = G5 == 0 ? 0 : A4 - G6 - G4;
            double G9 = 1.03;
            double S4 = 1.05;
            double T4 = 1.4;
            double J4 = craneBeamIn.MaxWheelLoad;
            double D6 = Math.Round(S4 * T4 * J4 * 9.8, 2);
            double G8 = G4 < A4 / 2 ? G4 : A4 / 2;
            double D9 = G7 == 0 ? Math.Round(G9 * D6 * G6 / 2 / 1000, 0) : Math.Round(G9 * D6 * G6 * (G8 + 2 * G7) / A4 / 1000, 0);
            double K4 = craneBeamIn.BeamHeight;
            double M4 = craneBeamIn.SY_Width;
            double P4 = craneBeamIn.Aperture;
            double R4 = M4 - 2 * P4;
            double O4 = craneBeamIn.YY_Thickness;
            double N4 = craneBeamIn.XY_Width;
            double L4 = craneBeamIn.FB_Thickness;
            double O7 = ((R4 + N4) * O4 + (K4 - O4 * 2) * L4) / 100;
            double O8 = Math.Round((R4 * O4 * (K4 - O4 / 2) + N4 * Math.Pow(O4, 2) / 2 + (K4 - 2 * O4) * L4 * K4 / 2) / O7 / 1000, 2);
            double O9 = Math.Round((R4 * Math.Pow(O4, 3) / 12 + R4 * O4 * Math.Pow((K4 - O8 * 10 - O4 / 2), 2) + N4 * Math.Pow(O4, 3) / 12 + N4 * O4 * Math.Pow((O8 * 10 - O4 / 2), 2) + L4 * Math.Pow((K4 - 2 * O4), 3) / 12 + (K4 - 2 * O4) * L4 * Math.Pow((O8 * 10 - K4 / 2), 2)) / 10000, 0);
            double O10 = Math.Round(O9 / (K4 - O8 * 10) * 10, 0);
            double B4 = craneBeamIn.ZJ_Weight;
            double H4 = craneBeamIn.XC_Weight;
            double D7 = Math.Round(T4 * 0.05 * (B4 + H4) * 9.8 / 2, 2);
            double D12 = Math.Round(D7 * D9 / D6, 2);
            double R8 = Math.Round(O4 * Math.Pow(M4, 3) / 12 / 10000, 0);
            double Q4 = craneBeamIn.CenterDistance;
            double R9 = Math.Round(R8 - 2 * P4 * O4 * Math.Pow((Q4 / 2), 2) / 10000, 0);
            double R10 = Math.Round(R9 / (M4 / 20), 0);
            double D21 = Math.Round((D9 / O10 + D12 / R10) * 1000, 0);
            string J6 = craneBeamIn.SteelMarks;
            double G11 = J6 == "Q235" ? 215 : 310;
            double C46 = craneBeamIn.JJL_Width;
            double E46 = craneBeamIn.JJL_Thickness;
            string J7 = craneBeamIn.BridgeCrane == true ? "Y" : "N";
            double E51 = craneBeamIn.JJL_φ;
            double I28 = craneBeamIn.HX_Spacing;

            CraneBeamOut craneBeamOut = new();

            craneBeamOut.QD_SYY = D21 < G11 ? "上翼缘正应力OK" : "上翼缘正应力NO";
            craneBeamOut.QD_SYYCalculated = D21;
            craneBeamOut.QD_SYYAdmissible = G11;
            //下翼缘正应力=IF(D22<G11,"OK","NO")
            double O11 = Math.Round(O9 / O8, 0);
            double D22 = Math.Round(D9 / O11 * 1000, 0);
            craneBeamOut.QD_XYY = D22 < G11 ? "下翼缘正应力OK" : "下翼缘正应力NO";
            craneBeamOut.QD_XYYCalculated = D22;
            craneBeamOut.QD_XYYAdmissible = D21;
            //平板支座剪应力=IF(D23<G12,"OK","NO")
            double D11 = Math.Round(G9 * (D6 * (A4 - G8) / A4 + D6), 0);
            double L7 = ((K4 - O4 * 2) * L4 + (M4 + N4) * O4) / 100;
            double L8 = Math.Round((M4 * O4 * (K4 - O4 / 2) + N4 * O4 * O4 / 2 + (K4 - O4 * 2) * L4 * K4 / 2) / L7 / 1000, 2);
            double L10 = Math.Round((M4 * O4 * (K4 - L8 * 10 - O4 / 2) + Math.Pow((K4 - L8 * 10 - O4), 2) * L4 / 2) / 1000, 0);
            double L9 = Math.Round((M4 * Math.Pow(O4, 3) / 12 + M4 * O4 * Math.Pow((K4 - L8 * 10 - O4 / 2), 2) + N4 * Math.Pow(O4, 3) / 12 + N4 * O4 * Math.Pow((L8 * 10 - O4 / 2), 2) + L4 * Math.Pow((K4 - O4 * 2), 3) / 12 + (K4 - O4 * 2) * L4 * Math.Pow((L8 * 10 - K4 / 2), 2)) / 10000, 0);
            double D23 = Math.Round(D11 * L10 / L9 / L4 * 100, 0);
            double G12 = J6 == "Q235" ? 125 : 180;
            craneBeamOut.QD_PB = D23 < G12 ? "平板支座剪应力OK" : "平板支座剪应力NO";
            craneBeamOut.QD_PBCalculated = D23;
            craneBeamOut.QD_PBAdmissible = G12;
            //腹板局部压应力=IF(D24<G11,"OK","NO")
            double G18 = 1;
            double G17 = 0;
            string J8 = craneBeamIn.OrbitModel;
            switch (J8)
            {
                case "QU70":
                    G17 = 120;
                    break;
                case "QU80":
                    G17 = 130;
                    break;
                case "QU100":
                    G17 = 120;
                    break;
                case "QU120":
                    G17 = 170;
                    break;
                case "38KG":
                    G17 = 134;
                    break;
                case "43KG":
                    G17 = 140;
                    break;
                case "50KG":
                    G17 = 152;
                    break;
                case "60KG":
                    G17 = 176;
                    break;
            }
            double D25 = 50 + 5 * O4 + 2 * G17;
            double D24 = Math.Round(G18 * D6 * 1000 / L4 / D25, 0);
            craneBeamOut.QD_FB = D24 < G11 ? "腹板局部压应力OK" : "腹板局部压应力NO";
            craneBeamOut.QD_FBCalculated = D24;
            craneBeamOut.QD_FBAdmissible = G11;
            //整体稳定性=IF(D39<=G11,"整体稳定性OK","整体稳定性NO")
            double D30 = Math.Round(O4 * Math.Pow(M4, 3) / 12 / 10000, 0);
            double D31 = Math.Round(O4 * Math.Pow(N4, 3) / 12 / 10000, 0);
            double D32 = Math.Round(D30 / (D30 + D31), 3);
            double D28 = Math.Round(A4 * O4 / M4 / K4, 3);
            double D29 = D28 <= 2 ? Math.Round(0.73 + 0.18 * D28, 3) : 1.09;
            double D33 = D32 > 0.8 ? (D28 <= 0.5 ? Math.Round(0.9 * D29, 3) : Math.Round(0.95 * D29, 3)) : D28;
            double D35 = Math.Round(Math.Pow(((D30 + D31) / L7), 0.5), 2);
            double D36 = Math.Round(A4 / D35 / 10, 1);
            double L11 = Math.Round(L9 / (K4 - L8 * 10) * 10, 0);
            double D34 = Math.Round(0.8 * (2 * D32 - 1), 3);
            double L19 = J6 == "Q235" ? 235 : 345;
            double D37 = Math.Round(D33 * 4320 * L7 * K4 / Math.Pow(D36, 2) / L11 / 10 * (Math.Pow((1 + Math.Pow((D36 * O4 / 4.4 / K4), 2)), 0.5) + D34) * 235 / L19, 2);
            double D38 = D37 < 0.6 ? D37 : Math.Round(1.07 - 0.282 / D37, 2);
            double R11 = Math.Round(R8 / (M4 / 20), 0);
            double D39 = Math.Round((D9 / D38 / L11 + D12 / R11) * 1000, 2);
            craneBeamOut.ZTWDX = D39 <= G11 ? "整体稳定性OK" : "整体稳定性NO";
            craneBeamOut.ZTWDX_Calculated = D39;
            craneBeamOut.ZTWDX_Admissible = G11;
            //挠度判断=IF(D44<1/G13,"挠度OK","挠度NO")
            //G13=IF(J7="Y",1000,500)
            double D43 = Math.Round(D9 / S4 / T4, 1);
            double D42 = Math.Round(D43 * Math.Pow(A4, 2) * 1000000 / (10 * 2.06 * 100000 * L9 * 10000), 2);
            double D44 = Math.Round(D42 / A4, 5);
            double G13 = J7 == "Y" ? 1000 : 500;
            craneBeamOut.ND = D44 < (1 / G13) ? "挠度OK" : "挠度NO";
            craneBeamOut.ND_Calculated = D44;
            craneBeamOut.ND_Admissible = G13;
            //支座加劲肋端面承压=IF(D47<L15,"OK","NO")
            double D47 = Math.Round(D11 * 1000 / (2 * (C46 - 15) * E46), 1);
            double L15 = J6 == "Q235" ? 325 : 400;
            craneBeamOut.DMCY = D47 < L15 ? "端面承压OK" : "端面承压NO";
            craneBeamOut.DMCY_Calculated = D47;
            craneBeamOut.DMCY_Admissible = L15;
            //支座加劲肋稳定性=IF(D52<G11,"OK","NO")
            double E48 = (40 + 10 + 15 * L4) * L4 + 2 * C46 * E46;
            double D52 = Math.Round(D11 * 1000 / E51 / E48, 0);
            craneBeamOut.JJLWDX = D52 < G11 ? "稳定性OK" : "稳定性NO";
            craneBeamOut.JJLWDX_Calculated = D52;
            craneBeamOut.JJLWDX_Admissible = G11;
            //上翼缘与腹板的连接焊缝=IF(D55<8,8,D55+2)
            double L18 = J6 == "Q235" ? 160 : 200;
            double D55 = Math.Round(1 / (2 * 0.7 * L18) * Math.Pow(Math.Pow((D11 * 1000 * M4 * O4 * (K4 - L8 * 10 - O4 / 2) * 1000 / L9 / 10000000), 2) + Math.Pow((G18 * D6 * 1000 / D25), 2), 0.5), 1);
            double D56 = D55 < 8 ? 8 : (D55 + 2);
            craneBeamOut.SYYHF = D56;
            //下翼缘与腹板的连接焊缝=IF(D58<6,6,D58+2)
            double D58 = Math.Round(D11 * N4 * O4 * (L8 * 10 - O4 / 2) / (2 * 0.7 * L18 * L9) / 10, 1);
            double D59 = D58 < 6 ? 6 : (D58 + 2);
            craneBeamOut.XYYHF = D59;
            //支座加劲肋与腹板的连接焊缝=IF(D61<8,8,D61+2)
            double D61 = Math.Round(D11 * 1000 / (0.7 * 4 * (K4 - 2 * O4 - 2 * 15 - 2 * L4) * L18), 2);
            double D62 = D61 < 8 ? 8 : (D61 + 2);
            craneBeamOut.JJLHF = D62;
            //配置横向加劲肋=IF(J6="Q235",IF(I27<=80,"宜配横肋","须配横肋"),IF(J6="Q345",IF(I27<=66,"宜配横肋","须配横肋")))
            double I27 = (K4 - 2 * O4) / L4;
            craneBeamOut.PZHL = J6 == "Q235" ? (I27 <= 80 ? "宜配横肋" : "须配横肋") : (I27 <= 66 ? "宜配横肋" : "须配横肋");
            //labelControl293.ForeColor = J6 == "Q235" ? (I27 <= 80 ? Color.DarkGreen : Color.Red) : (I27 <= 66 ? Color.DarkGreen : Color.Red);
            //腹板局部稳定性=IF(J44<=1,"稳定性OK","稳定性NO")
            double Q13 = K4 - L8 * 10 - O4;
            double J31 = Math.Round(D9 * Q13 / L9 * 100, 1);
            double J36 = Math.Round(2 * Q13 / L4 / 153 * Math.Pow((L19 / 235), 0.5), 3);
            double J38 = J36 <= 0.85 ? G11 : (J36 > 1.25 ? 1.1 * G11 / Math.Pow(J36, 2) : (1 - 0.75 * (J36 - 0.85)) * G11);
            double D10 = Math.Round(G9 * 2 * D6 * (A4 / 2 - G5) / A4, 0);
            double J33 = Math.Round(D10 / (K4 - O4 * 2) / L4 * 1000, 1);
            double I39 = Math.Round(I28 / (K4 - 2 * O4), 3);
            double G39 = K4 - 2 * O4;
            double G40 = G39 / I28;
            double I40 = I39 <= 1 ? Math.Round(G39 / L4 / (41 * Math.Pow((4 + 5.34 * Math.Pow(G40, 2)), 0.5)) * Math.Pow((L19 / 235), 0.5), 3) : Math.Round(G39 / L4 / (41 * Math.Pow((5.34 + 4 * Math.Pow(G40, 2)), 0.5)) * Math.Pow((L19 / 235), 0.5), 3);
            double J41 = I40 <= 0.8 ? G12 : (I40 > 1.2 ? Math.Round(1.1 * G12 / Math.Pow(I40, 2), 3) : Math.Round((1 - 0.59 * (I40 - 0.8)) * G12, 3));
            double J35 = Math.Round(0.9 * D6 / D25 / L4 * 1000, 1);
            double I42 = 0;
            if (I39 > 0.5)
            {
                if (I39 <= 1.5)
                {
                    I42 = Math.Round(G39 / L4 / (28 * Math.Pow(10.9 + 13.4 * Math.Pow((1.83 - I28 / G39), 3), 0.5)) * Math.Pow((L19 / 235), 0.5), 3);
                }
                else if (I39 <= 2)
                {
                    I42 = Math.Round(G39 / L4 / (28 * Math.Pow((18.9 - 5 * I28 / G39), 0.5)) * Math.Pow((L19 * 235), 0.5), 3);
                }
            }
            double F11 = 0;
            double J43 = I42 <= 0.9 ? G11 : (I42 > 1.2 ? Math.Round(1.1 * F11 / Math.Pow(I42, 2), 3) : Math.Round((1 - 0.79 * (I42 - 0.9)) * G11, 3));
            double J44 = Math.Round(Math.Pow((J31 / J38), 2) + Math.Pow((J33 / J41), 2) + J35 / J43, 2);
            craneBeamOut.FBWDX = J44 <= 1 ? "腹板局部稳定性OK" : "腹板局部稳定性NO";
            craneBeamOut.FBWDX_Calculated = J44;

            return craneBeamOut;
        }
    }
}
